/**
 * @(#)HomeActivity.java
 * 
 * @Version: 1
 * @JDK: jdk 1.6.0.XXX
 * @Module: MBA
 */
/*- 				History
 **********************************************
 *  ID      DATE           PERSON       REASON
 *  1     2013-03-04     Yongkai.Yu    Created
 **********************************************
 */

package com.mba.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.mba.adapter.HomeListAdapter;
import com.mba.base.BaseActivity;
import com.mba.bean.Message;
import com.mba.service.MessageService;
import com.mba.util.Tools;

/**
 * 首页Activity
 */
public class HomeActivity extends BaseActivity {

	private static final String TAG = "HomeActivity";

	private LinearLayout load_progress = null;
	private ListView home_lv = null;
	// 保存需要显示的多条消息数据
	public ArrayList<Message> contentList = null;

	// private HomeAdapters adapater = null;
	private Tools tools;
	private MessageService msgService;
	private RadioGroup switcherGroup;
	private Button create_msg_btn;

	// 使用GridView定义的自定义菜单
	private GridView gv_foot_menu;

	@Override
	protected void result(Object o) {
		super.result(o);
		System.out.println(o);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.home);
		this.load_progress = (LinearLayout) findViewById(R.id.load_progress);

		this.home_lv = (ListView) findViewById(R.id.home_lv);

		
		
		/** --------------------------------------------------------- */

		/*
		 * View v= (View) home_lv.getItemAtPosition(0); v.setOnClickListener(new
		 * OnClickListener() {
		 * 
		 * @Override public void onClick(View arg0) { // TODO Auto-generated
		 * method stub Intent intent = new
		 * Intent(HomeActivity.this,UserMainPageActivity.class);
		 * startActivity(intent); } });
		 */

		this.switcherGroup = (RadioGroup) findViewById(R.id.switcher_group);

		this.create_msg_btn = (Button) findViewById(R.id.create_msg_btn);
		create_msg_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(HomeActivity.this,
						MessageAnnouceActivity.class);
				startActivity(intent);
			}
		});
		HomeClick click = new HomeClick();

		// 切换标签监听器
		SwitcherListener switcherListener = new SwitcherListener();
		this.switcherGroup.setOnCheckedChangeListener(switcherListener);

		init();
	}

	/**
	 * 加载首页数据
	 */
	public void init() {
		this.tools = Tools.getInstance();
		
		this.msgService = MessageService.getInstance();
		// 获得主要显示的数据 标志位以后用于读取本地还是社区
		try {
			this.contentList = (ArrayList<Message>) this.msgService.getHomeTimeline(Tools.GET_LOCAL);
		} catch (Exception e) {
			e.printStackTrace();
		}

		TextView title = (TextView) findViewById(R.id.main_title_text);
		title.setText("雨打梨花");

		if (this.contentList != null) {
			// 创建一个Adapter设置ListView中的每项Item项数据
			// this.adapater = new HomeAdapters(this, contentList);
			// 设置listview上的item点击事件处理

			this.home_lv.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					Object obj = parent.getTag();
					if (obj != null) {
						// 获得一条weibo数据的id（唯一标识）
						String weiboId = obj.toString();
						// 跳转到一条具体的微博显示页面
						Intent intent = new Intent(HomeActivity.this,
								ContentActivity.class);
						Bundle bundle = new Bundle();
						// 参数的设置
						bundle.putString("weiboId", weiboId);
						bundle.putInt("position", position);
						// 参数绑定
						intent.putExtras(bundle);
						startActivity(intent);
					}

				}
			});
			// 将adapter和listview关联
			this.home_lv.setAdapter(new HomeListAdapter(this, contentList));
		}
		// 隐藏进度条
		this.load_progress.setVisibility(View.GONE);
	}

	class HomeClick implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.create_msg_btn:
				break;
			case R.id.show_local:

				break;
			}
		}
	}

	class SwitcherListener implements OnCheckedChangeListener {

		private RadioButton localButton;
		private RadioButton forumButton;

		public void onCheckedChanged(RadioGroup group, int checkedId) {

			localButton = (RadioButton) findViewById(R.id.show_local);
			forumButton = (RadioButton) findViewById(R.id.show_forum);

			switch (checkedId) {
			case R.id.show_local:
				forumButton.setTextColor(getResources().getColor(
						R.color.swither_text_blur));
				localButton.setTextColor(getResources().getColor(
						R.color.swither_text_focus));
				break;

			case R.id.show_forum:
				localButton.setTextColor(getResources().getColor(
						R.color.swither_text_blur));
				forumButton.setTextColor(getResources().getColor(
						R.color.swither_text_focus));
				break;
			}

		}

	}

}
